//uniform float sphere_radius;

//varying vec3 vertex_light_position;
//varying vec4 eye_position;
varying vec4 sphere_diffuse_color;

void main()
{
    vec3 vertex_light_position = normalize(gl_LightSource[0].position.xyz);
    //vec4 eye_position = gl_Position;
    vec3 normal, lightDir, viewVector, halfVector;
    vec3 diffuse, ambient, globalAmbient, specular = vec3(0.0);
    float NdotL,NdotHV;

    vec3 color;

    vec2 pos2 = gl_TexCoord[0].xy - vec2(0.5, 0.5);
    if(length(pos2) > 0.5)
        discard;
    float r = 0.5;
    float x = pos2.x;
    float y = pos2.y;
    float z = sqrt(r*r - x*x - y*y);

    normal = normalize(vec3(x, y, z));
    /*lightDir = normalize(vec3(gl_LightSource[0].position));*/
    NdotL = max(dot(normal, vertex_light_position), 0.0);
    //vec4 pos = eye_position;
    //pos.z += z*sphere_radius;
    //pos = gl_ProjectionMatrix * pos;
    //gl_FragDepth = (pos.z / pos.w + 1.0) / 2.0;

    //diffuse = gl_FrontMaterial.diffuse.xyz * gl_LightSource[0].diffuse.xyz;
    diffuse = sphere_diffuse_color.xyz * gl_LightSource[0].diffuse.xyz;
    ambient = gl_FrontMaterial.ambient.xyz * gl_LightSource[0].ambient.xyz;
    globalAmbient = gl_LightModel.ambient.xyz * gl_FrontMaterial.ambient.xyz;

    color=ambient+globalAmbient;

    if (NdotL > 0.0) {
       color+=diffuse*NdotL;
       NdotHV = max(dot(normal, normalize(gl_LightSource[0].halfVector.xyz)),0.0);
       specular = gl_FrontMaterial.specular.xyz * gl_LightSource[0].specular.xyz * pow(NdotHV,gl_FrontMaterial.shininess);
       color+=specular;
    }

    gl_FragColor = vec4(color,1.0);
    //gl_FragColor = gl_Color * NdotL;
}
